import {
	theme
} from '@/setting'

const state = {
	type: localStorage.getItem('theme') || 'light'
}

const mutations = {
	THEME_COLOR: (state, data) => {
		state.type = data
	}
}

const actions = {
	setTheme({
		commit
	}, data) {
		let type = 'light';
		if (data != null) {
			type = data;
		} else {
			let index = theme.findIndex(item => {
				return item === state.type
			});
			if (++index > theme.length - 1) {
				index = 0;
			}
			type = theme[index];
		}
		localStorage.setItem('theme', type)
		commit('THEME_COLOR', type)
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
